package org.example.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
public class BankCardDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 更新余额
    public int updateBalance(long cardId, BigDecimal delta) {
        String sql = "UPDATE bank_card SET balance = balance + ? WHERE id = ?";
        return jdbcTemplate.update(sql, delta, cardId);
    }

    // 查询余额
    public BigDecimal getBalance(long cardId) {
        String sql = "SELECT balance FROM bank_card WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, BigDecimal.class, cardId);
    }
}

